package com.heyqing.topic.SummaryRanges_0228;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:SummaryRanges
 * Package:com.heyqing.topic.SummaryRanges_0228
 * Description:
 *
 * @Date:2024/9/23
 * @Author:Heyqing
 */
public class SummaryRanges {
    public List<String> summaryRanges(int[] nums) {
        List<String> resultList = new ArrayList<>();
        if (nums.length == 1) {
            resultList.add(String.valueOf(nums[0]));
            return resultList;
        }
        String separator = "->";
        int startIndex = 0;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] != (nums[i - 1] + 1)) {
                if (startIndex == (i - 1)) resultList.add(String.valueOf(nums[startIndex]));
                else resultList.add(nums[startIndex] + separator + nums[i - 1]);
                startIndex = i;
            }
            if (i == nums.length - 1) {
                if (startIndex == i) resultList.add(String.valueOf(nums[startIndex]));
                else resultList.add(nums[startIndex] + separator + nums[i]);
            }
        }
        return resultList;
    }
}
